//
//  UserModel.m
//  BasicFramework
//
//  Created by 利君 on 2017/6/13.
//  Copyright © 2017年 LiJun All rights reserved.
//

#import "UserModel.h"
static UserModel *userInfo = nil;
@implementation UserModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"ID":@"id"};
}
+ (instancetype)sharedUserInfo {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        userInfo = [[UserModel alloc] init];
//    });
    return userInfo;
}
+ (void)setUserInfoModelWithTokenDict:(NSDictionary *)dict {
    if (dict == nil){
        return;
    }
    UserModel *model = [UserModel mj_objectWithKeyValues:dict];
    userInfo = model;
}
+ (void)setUserInfoModelWithDict:(NSDictionary *)dict{
    if (dict == nil){
        return;
    }
    UserModel *model = [UserModel mj_objectWithKeyValues:dict];
    model.token = userInfo.token;
    userInfo = model;
    model.level = model.level;
    [AccessTool saveUserInfo];
}

+ (void)resetModel:(UserModel *)model {
    userInfo = model;
}
- (id)initWithCoder:(NSCoder *)deCoder {
    if (self = [super init]) {
        for (NSString *key in [self getPropertyList:[UserModel class]]) {
            id value = [deCoder decodeObjectForKey:key];
            if (value) {
                [self setValue:value forKey:key];
            }
        }
//        self.isset_pay_password = [deCoder decodeObjectForKey:@"isset_pay_password"];
//        self.order_b = [deCoder decodeObjectForKey:@"order_b"];
//        self.ID = [deCoder decodeObjectForKey:@"id"];
//        self.head_img = [deCoder decodeObjectForKey:@"head_img"];
//        self.order_d = [deCoder decodeObjectForKey:@"order_d"];
//   self.order_a = [deCoder decodeObjectForKey:@"order_a"];
//        self.grade_id = [deCoder decodeObjectForKey:@"grade_id"];
//        self.price = [deCoder decodeObjectForKey:@"price"];
//
//        self.level = [deCoder decodeObjectForKey:@"level"];
//          self.order_c = [deCoder decodeObjectForKey:@"order_c"];
//        self.tel = [deCoder decodeObjectForKey:@"tel"];
//        self.coupon_count = [deCoder decodeObjectForKey:@"coupon_count"];
//        self.nickname = [deCoder decodeObjectForKey:@"nickname"];
//        self.token = [deCoder decodeObjectForKey:@"token"];
//        self.is_member = [deCoder decodeObjectForKey:@"is_member"];

    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)enCoder {
    for (NSString *key in [self getPropertyList:[self class]]) {
        id value = [self valueForKey:key];
        [enCoder encodeObject:value forKey:key];
    }
//    [enCoder encodeObject:_isset_pay_password forKey:@"isset_pay_password"];
//    [enCoder encodeObject:_order_b forKey:@"order_b"];
//        [enCoder encodeObject:_ID forKey:@"id"];
//    [enCoder encodeObject:_head_img forKey:@"head_img"];
//    [enCoder encodeObject:_order_d forKey:@"order_d"];
//    [enCoder encodeObject:_order_a forKey:@"order_a"];
//    [enCoder encodeObject:_grade_id forKey:@"grade_id"];
//    [enCoder encodeObject:_price forKey:@"price"];
//    [enCoder encodeObject:_level  forKey:@"level"];
//    [enCoder encodeObject:_order_c forKey:@"order_c"];
//    [enCoder encodeObject:_point forKey:@"point"];
//    [enCoder encodeObject:_tel forKey:@"tel"];
//    [enCoder encodeObject:_coupon_count forKey:@"coupon_count"];
//    [enCoder encodeObject:_nickname forKey:@"nickname"];
//    [enCoder encodeObject:_token forKey:@"token"];
//    [enCoder encodeObject:_is_member forKey:@"is_member"];
}

/// 获取类的属性列表

- (NSArray *)getPropertyList:(Class)klass {
    NSMutableArray *propertyNamesArray = [NSMutableArray array];
    unsigned int propertyCount = 0;
    objc_property_t *properties = class_copyPropertyList(klass, &propertyCount);
    for (unsigned int i = 0; i < propertyCount; ++i) {
        objc_property_t property = properties[i];
//        const char * name = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:property_getName(property)];
        //遵守协议就会出现
        NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"];
        if ([filters containsObject:name]) {
            continue;
        }
        [propertyNamesArray addObject:name];
    }
    free(properties);
    return propertyNamesArray;
}

//- (NSString *)token {
//    return @"102117ed8c517bdee62e109db2645e90";
//}


@end
@implementation LeverModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"ID":@"id"};
}
@end
